local shanxi = fk.CreateSkill {

  name = "joy__shanxi",

  tags = {  },

}



shanxi:addEffect("active", {
  name = "joy__shanxi",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = function ()
    local tempSlots = table.simpleClone(player.equipSlots)
    local tempSealedSlots = table.simpleClone(player.sealedSlots)
    for _, sealedSlot in ipairs(tempSealedSlots) do
      table.removeOne(tempSlots, sealedSlot)
    end
    local n = 1 + #tempSlots - #player:getCardIds(Player.Equip)
    return "#joy__shanxi-choose:::"..n
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(shanxi.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and not target:inMyAttackRange(player) and to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local card_data = {}
    local to_hands = {}
    for i = 1, to:getHandcardNum() do
      table.insert(to_hands, -1)
    end
    if #to_hands > 0 then
      table.insert(card_data, { to.general, to_hands })
    end
    if not player:isKongcheng() then
      table.insert(card_data, { player.general, player.player_cards[Player.Hand] })
    end
    if #card_data == 0 then return end
    local cards = room:askForPoxi(player, "joy__shanxi_show", card_data, nil, true)
    if #cards == 0 then return end
    local from_cards = table.filter(cards, function(id) return id ~= -1 end)
    local to_cards = (#from_cards == #cards) and {} or table.random(to:getCardIds("h"), #cards-#from_cards)
    player:showCards(from_cards)
    to:showCards(to_cards)
    cards = table.connect(from_cards, to_cards)
    local to_throw = table.filter(cards, function (id)
      return Fk:getCardById(id).name == "jink"
    end)
    if #to_throw > 0 then
      local moveInfos = {}
      local to_throw1 = table.filter(to_throw, function(id) return table.contains(from_cards, id)
      and not player:prohibitDiscard(Fk:getCardById(id)) end)
      if #to_throw1 > 0 then
        table.insert(moveInfos, {
          from = player.id,
          ids = to_throw1,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonDiscard,
          proposer = player.id,
          skillName = shanxi.name,
        })
      end
      local to_throw2 = table.filter(to_throw, function(id) return table.contains(to_cards, id) end)
      if #to_throw2 > 0 then
        table.insert(moveInfos, {
          from = to.id,
          ids = to_throw2,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonDiscard,
          proposer = player.id,
          skillName = shanxi.name,
        })
      end
      if #moveInfos > 0 then
        room:moveCards(table.unpack(moveInfos))
      end
      if player.dead or to.dead then return end
      card_data = {}
      if #to:getCardIds("e") > 0 then
        table.insert(card_data, { "$Equip", to:getCardIds("e") })
      end
      local nonshow = table.filter(to:getCardIds("h"), function(id) return not table.contains(to_cards, id) end)
      if #nonshow > 0 then
        to_hands = {}
        for i = 1, #nonshow do
          table.insert(to_hands, -1)
        end
        table.insert(card_data, { "$Hand", to_hands })
      end
      if #card_data == 0 then return end
      local card = room:askForCardChosen(player, to, { card_data = card_data }, shanxi.name)
      if room:getCardArea(card) ~= Card.PlayerEquip then
        card = table.random(nonshow)
      end
      room:obtainCard(player, card, false, fk.ReasonPrey)
    end
  end,
})

return shanxi